package leetcode;

import java.util.Arrays;

/**
 * @Project : AlgorithmLearning
 * @Package : Leetcode
 * @File : Q0097.java
 * @Author : WangRuoyu
 * @Date 2023/11/6 20:36
 */


public class Q0977 {
    public int[] sortedSquares(int[] nums) {
        int[] res = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            nums[i] *= nums[i];
        }
        int l = 0, r = nums.length - 1;
        int k = nums.length - 1;
        while (l <= r) {
            if (nums[l] >= nums[r]) {
                res[k--] = nums[l++];
            } else {
                res[k--] = nums[r--];
            }
        }
        return res;
    }

    public static void main(String[] args) {
        Q0977 sl = new Q0977();
        int[] nums = {-4, -1, 0, 3, 10};
        System.out.println(Arrays.toString(sl.sortedSquares(nums)));
    }
}